import { StateMoal } from "./StateMoal";
import { type RemoveMethods } from "@/types/remove-methods";

export class FieldIdData {
  id: number;
  children: Array<FieldIdData>;

  constructor(id: number, children: Array<FieldIdData>) {
    this.id = id;
    this.children = children;
  }
}
export class FieldContentData {
  id: number;
  topic: string;
  direction: string;
  markDownFile: string;
  stageIds: StateMoal;

  constructor(id: number, topic: string, stageIds: StateMoal) {
    this.id = id;
    this.topic = topic;
    this.direction = "left";
    this.markDownFile = "";
    this.stageIds = stageIds;
  }
  static checkIsGoing(data: FieldContentData) {
    return data.stageIds.onGonging.length > 0;
  }
}

export class MindMapData {
  id: string;
  topic: string;
  direction: string;
  expanded: boolean;
  children: Array<MindMapData>;

  constructor(id: string, topic: string, direction: string, expanded: boolean, children: Array<MindMapData>) {
    this.id = id;
    this.topic = topic;
    this.direction = direction;
    this.expanded = expanded;
    this.children = children;
  }
}

export class FieldTree {
  fieldTree: Array<FieldIdData>;
  fieldData: Array<FieldContentData>;
  defaultExpandedKeys: Array<number | string>;
  setInfo(data: RemoveMethods<FieldTree>) {
    Object.assign(this, data);
  }

  constructor(fieldTree: Array<FieldIdData>, fieldData: Array<FieldContentData>) {
    this.fieldTree = fieldTree;
    this.fieldData = fieldData;
    this.defaultExpandedKeys = [];
  }

  getMindMapData() {
    const result = new Array<MindMapData>();
    const map = new Map<number, FieldContentData>();
    for (const fieldDatum of this.fieldData) {
      map.set(fieldDatum.id, fieldDatum);
    }
    const getMindMap = (result: Array<MindMapData>, list: Array<FieldIdData>) => {
      for (let i = 0; i < list.length; i++) {
        const fieldIdDatum = list[i];
        const fieldId = fieldIdDatum.id;
        const fieldData = map.get(fieldId);
        if (fieldData !== undefined) {
          const children = fieldIdDatum.children;
          const resultChild = new Array<MindMapData>();
          getMindMap(resultChild, children);
          result.push(
            new MindMapData(fieldData.id.toString(), fieldData.topic, i % 2 ? "left" : "right", true, resultChild),
          );
        }
      }
    };
    getMindMap(result, this.fieldTree);
    return new MindMapData("全部领域", "全部领域", "", true, result);
  }

  getFieldDataById(id: number | undefined | string | null) {
    return this.fieldData.find(value => value.id === id);
  }
  // checkCanDelete(id: number | undefined) {
  //   if (id === undefined) return true;
  //   const result = new Set<number>();
  //   const data = this.fieldData.find(value => value.id === id);
  //   if (data !== undefined) StateMoal.getAllId(data.stageIds, result, true, true, true);
  //   return result.size === 0;
  // }
  deleteFieldData(id: number | undefined) {
    let i = -1;
    this.fieldData.find((value, index) => {
      if (value.id === id) i = index;
    });
    if (i !== -1) {
      this.fieldData.splice(i, 1)[0];
    }
    //this.fieldData = this.fieldData.filter(item => item.id !== id);
  }
  addFieldData(data: FieldContentData) {
    this.fieldData.push(data);
  }
  setFieldIdDataList(data: Array<FieldIdData>) {
    this.fieldTree = data;
  }
  addFieldIdData(data: FieldIdData) {
    this.fieldTree.push(data);
  }
  setDefaultExpandedKeys(defaultExpandedKeys: Array<number | string>) {
    this.defaultExpandedKeys = defaultExpandedKeys;
  }
  getFieldSelectOptions() {
    return this.fieldData.map(s => {
      return {
        label: s.topic,
        value: s.id,
      };
    });
  }
  getFieldSelectOptionsWithLimit(chooseFields: Array<number>) {
    const set = new Set<number>();
    for (const chooseField of chooseFields) {
      set.add(chooseField);
    }
    const result = new Array<{ label: string; value: number }>();
    for (const fieldDatum of this.fieldData) {
      if (set.has(fieldDatum.id))
        result.push({
          label: fieldDatum.topic,
          value: fieldDatum.id,
        });
    }
    return result;
  }
}
